<?php
namespace app\app\util;
use think\cache\driver\Redis;
use think\Request;

//闯关排名
define('Block_rank','Answer_block');
//每日分享次数
define('Share','Answer_share:id=');
//每日答对题目数
define('Answer','Answer_number:id=');
//好友对战题目
define('Fight','Answer_fight:room=');
//战胜好友数
define('Win','Answer_win:id=');


class BlockUtils{

    //创建闯关数
    public static function foundUserBlock($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        //创建用户
        return $redis->zadd(Block_rank,$number,$user_id);
    }

    //获取闯关数
    public static function getUserBlock($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($number == 0){
            return $redis->zscore(Block_rank,$user_id);
        }else{
            $result = $redis->zincrby(Block_rank,$number,$user_id);
            return $result;
        }
    }

    //获取指定范围排名
    public static function getBlockRank($start,$end,$type='')
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        return $redis->zrevrange(Block_rank,$start,$end,$type);
    }

    /**
     * 获取用户排名
     * @param $user_id
     */
    public static function getUserRank($user_id){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        return $redis->zrevrank(Block_rank,$user_id);
    }


    /**
     * 设置每日分享次数
     */
    public static function setShare($key,$value,$overdatetime){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Share.$key)){
            $redis->rm(Share.$key);
        }
        $redis->set(Share.$key,$value,$overdatetime);
        return true;
    }

    /**
     * 获取每日分享次数
     */
    public static function getShare($key){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Share.$key)){
            return $redis->get(Share.$key);
        }else{
            return 0;
        }
    }

    /**
     * 设置每日答对题次数
     */
    public static function setAnswer($key,$value,$overdatetime){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Answer.$key)){
            $redis->rm(Answer.$key);
        }
        $redis->set(Answer.$key,$value,$overdatetime);
        return true;
    }

    /**
     * 获取每日答对题次数
     */
    public static function getAnswer($key){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Answer.$key)){
            return $redis->get(Answer.$key);
        }else{
            return 0;
        }
    }

    /**
     * 设置每日战胜好友数
     */
    public static function setWin($key,$value,$overdatetime){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Win.$key)){
            $redis->rm(Win.$key);
        }
        $redis->set(Win.$key,$value,$overdatetime);
        return true;
    }

    /**
     * @param $key
     * 获取每日战胜好友数
     */
    public static function getWin($key){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Win.$key)){
            return $redis->get(Win.$key);
        }else{
            return 0;
        }
    }

    /**
     * 设置对战题目
     * @param $key
     * @param $value
     * @param $overtime
     */
    public static function setFight($key,$value,$overtime=36000){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Fight.$key)){
            $redis->rm(Fight.$key);
        }
        $redis->set(Fight.$key,$value,$overtime);
        return true;
    }

    /**
     * 获取对战题目
     * @param $key
     * @return int|mixed
     */
    public static function getFight($key){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($redis->has(Fight.$key)){
            return $redis->get(Fight.$key);
        }else{
            return 0;
        }
    }
	
	public static function delkey($key,$id){
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		return $redis->del($key.$id);
	}
	
	public static function getKey($key){
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		return $redis->keys($key);
	}
}